package org.code.mallspringboot.controller;

import org.code.mallspringboot.pojo.PmsSkuStock;
import org.code.mallspringboot.service.PmsProductService;
import org.code.mallspringboot.service.PmsSkuService;
import org.code.mallspringboot.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SkuController {

    @Autowired
    private PmsSkuService pmsSkuService;

    //SKU库存
    @GetMapping("/sku/{id}")
    public CommonResult sku(@PathVariable int id, String keyword){
        List<PmsSkuStock> stockList = pmsSkuService.selectByIdAndKeyword(id, keyword);
        if (stockList.size() > 0){
            return CommonResult.success(stockList);
        }
        return CommonResult.failed("查询失败!");
    }

    //修改SKU库存
    @PostMapping("/sku/update/{pid}")
    public CommonResult update(@PathVariable int pid,@RequestBody List<PmsSkuStock> stockList){
        boolean flag = false;
        for (PmsSkuStock stock : stockList) {
            if (pmsSkuService.update(stock.getId(),stock.getSkuCode(),stock.getPrice(),stock.getStock(),stock.getLowStock()) > 0) flag = true;
        }
        if (flag){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }
}
